package com.google.code.wicket.auth.component.value.resolver.annotation;

import java.lang.annotation.Annotation;

import com.google.code.wicket.auth.component.value.resolver.SingleValueResolver;

/**
 * An implementation of {@link SingleValueResolver} that gets a value from an
 * annotation using a defined strategy.
 * 
 * @param <A>
 * @param <T>
 */
public class SingleAnnotationValueResolver<A extends Annotation, T> extends
        SingleValueResolver<T> {

    private final AnnotationCache<A> cache;

    private final SingleAnnotationValueRetrievalStrategy<A, T> strategy;

    /**
     * Constructor with an annotation type and a
     * {@link SingleAnnotationValueRetrievalStrategy} to extract data from such
     * an annotation type.
     * 
     * @param annotationType
     * @param annotationValueRetrievalStrategy
     */
    public SingleAnnotationValueResolver(
            final Class<A> annotationType,
            final SingleAnnotationValueRetrievalStrategy<A, T> annotationValueRetrievalStrategy) {
        this.cache = AnnotationCache.forAnnotationType(annotationType);
        this.strategy = annotationValueRetrievalStrategy;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T getSingleValue(final Class<?> componentClass) {
        final A annotation = this.cache.getAnnotationForType(componentClass);
        return annotation == null ? null : this.strategy
                .retrieveAnnotationValue(annotation);
    }

}
